import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class code3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String str1 = in.next();
        String str2 = in.next();
        String[] str = new String[n];
        for(int i=0;i<n;i++) {
            str[i] = in.nextLine();
        }
        int prev1 = 0;
        int prev2 = 0;
        int ret = n;
        for(int i=0;i<n;i++) {
            if(str[i].equals(str1)) prev1 = i;
            if(str[i].equals(str2)) prev2 = i;
            if(str[prev1].equals(str1)&&str[prev2].equals(str2))  ret = Math.min(ret,Math.abs(prev1-prev2));

        }

        System.out.print(ret==n?-1:ret);
    }

}